#include "page3_widget.h"
#include "user/welcomewidget.h"

Page3_Widget::Page3_Widget(WelcomeWidget *widget)
{
    this->welcomewidget = widget;
    welcome_ui = this->welcomewidget->ui;

    mqtt_server_ip = welcome_ui->lineEdit_server_ip->text();
    mqtt_server_port = welcome_ui->lineEdit_server_port->text();
    mqtt_client = nullptr;

    connect(welcome_ui->pb_connect_mqtt,SIGNAL(clicked()),this,SLOT(on_pb_connect_mqtt_clicked()));      //连接MQTT服务器
    connect(welcome_ui->pb_publish,SIGNAL(clicked()),this,SLOT(on_pb_publish_clicked()));                //发布内容
    connect(welcome_ui->pb_sub_clear,SIGNAL(clicked()),this,SLOT(on_pb_sub_clear_clicked()));            //清除显示的内容
    connect(welcome_ui->pb_subscription,SIGNAL(clicked()),this,SLOT(on_pb_subscription_clicked()));      //开始订阅

    welcome_ui->pb_connect_mqtt->setCheckable(false);
    welcome_ui->pb_subscription->setCheckable(false);

    welcome_ui->pb_connect_mqtt->setEnabled(false);    //失能连接MQTT服务器按钮
    welcome_ui->pb_publish->setEnabled(false);         //失能发布按钮
    welcome_ui->pb_subscription->setEnabled(false);    //失能订阅按钮


    if((mqtt_server_ip != "") && (mqtt_server_port != ""))
    {
        bool ok;
        unsigned short mqtt_port = mqtt_server_port.toUShort(&ok);

        //mqtt_client = new QMQTT::Client(QHostAddress(mqtt_server_ip),mqtt_port);
        mqtt_client = new QMQTT::Client("mq.tongxinmao.com",mqtt_port,false,false,nullptr);

        if(mqtt_client)
        {
            mqtt_client->setClientId("clientid");
            //mqtt_client->setUsername("user");
            //mqtt_client->setPassword("password");

            connect(mqtt_client,SIGNAL(connected()),this,SLOT(slot_do_mqtt_connect()));
            connect(mqtt_client,SIGNAL(disconnected()),this,SLOT(slot_do_mqtt_disconnect()));
            connect(mqtt_client,SIGNAL(received(QMQTT::Message)),this,SLOT(slot_do_mqtt_data_recv(QMQTT::Message)));

            welcome_ui->pb_connect_mqtt->setEnabled(true);  //使能连接MQTT服务器按钮
        }
    }
}

Page3_Widget::~Page3_Widget()
{

}


//MQTT连接成功
void Page3_Widget::slot_do_mqtt_connect()
{
    qDebug() << ">>> slot_do_mqtt_connect >>>";
    welcome_ui->pb_publish->setEnabled(true);    //使能发布按钮
    welcome_ui->pb_subscription->setEnabled(true);   //使能订阅按钮

    welcome_ui->pb_connect_mqtt->setText(trUtf8("断开MQTT服务器"));
    welcome_ui->label_link_status->setText(trUtf8("MQTT服务器连接成功"));
}

//MQTT连接断开
void Page3_Widget::slot_do_mqtt_disconnect()
{
    qDebug() << ">>> slot_do_mqtt_disconnect >>>";

    welcome_ui->pb_connect_mqtt->setText(trUtf8("连接MQTT服务器"));
    welcome_ui->label_link_status->setText(trUtf8("MQTT服务器断开成功"));

    if(welcome_ui->pb_subscription->isCheckable())
    {
        welcome_ui->pb_subscription->setCheckable(false);
        welcome_ui->pb_subscription->setText("开始订阅");
    }

    welcome_ui->pb_publish->setEnabled(false);   //失能发布按钮
    welcome_ui->pb_subscription->setEnabled(false);   //失能订阅按钮
}

//MQTT收到数据
void Page3_Widget::slot_do_mqtt_data_recv(QMQTT::Message message)
{
    qDebug() << ">>> slot_do_mqtt_data_recv >>>";

    QString mes = QString("[topic]:")+message.topic()+"  "+QString("[payload]:")+message.payload();
    welcome_ui->textBrowser_subscription->append(mes);
}


//连接MQTT服务器
void Page3_Widget::on_pb_connect_mqtt_clicked()
{
    if(!mqtt_client->isConnectedToHost())
    {
        mqtt_client->connectToHost();
    }
    else
    {
        mqtt_client->disconnectFromHost();
    }
}

//发布内容
void Page3_Widget::on_pb_publish_clicked()
{
    QString topic = welcome_ui->lineEdit_pub_topic->text().trimmed();
    QString payload = welcome_ui->textEdit_publish->toPlainText().trimmed();

    if(topic.isEmpty() || payload.isEmpty())
    {
        qDebug()<<"pub topic and payload is empty!"<<endl;
        return;
    }
    QMQTT::Message message(136,topic,payload.toUtf8());

    mqtt_client->publish(message);      //发布消息
}

//清除显示的内容
void Page3_Widget::on_pb_sub_clear_clicked()
{
    welcome_ui->textBrowser_subscription->clear();
}

//开始订阅
void Page3_Widget::on_pb_subscription_clicked()
{
    if(welcome_ui->pb_subscription->isCheckable())
    {
        welcome_ui->pb_subscription->setCheckable(false);

        QString topic = welcome_ui->lineEdit_sub_topic->text().trimmed();

        if(topic.isEmpty())
        {
            qDebug()<<"sub topic and payload is empty!";
            return;
        }

        mqtt_client->unsubscribe(topic);     //停止订阅
        welcome_ui->pb_subscription->setText("开始订阅");
    }
    else
    {
        welcome_ui->pb_subscription->setCheckable(true);

        QString topic = welcome_ui->lineEdit_sub_topic->text().trimmed();

        if(topic.isEmpty())
        {
            qDebug()<<"sub topic and payload is empty!";
            return;
        }
        qDebug() << topic;
        mqtt_client->subscribe(topic);
        welcome_ui->pb_subscription->setText("停止订阅");
    }
}







